package it.sportingnola.soccernotes.adapter;

import it.sportingnola.soccernotes.Const;
import it.sportingnola.soccernotes.R;
import it.sportingnola.soccernotes.SubstEdit;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class SubstAdapter extends AbstractCursorAdapter {

    public SubstAdapter(SubstEdit context, int layout, Cursor c, String[] from, int[] to) {
	super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
	final int count=myTo.length;
	final int[] from=findColumns(myFrom);
	final int[] to=myTo;
	Resources r=context.getResources();
	String text;
	for (int i=0; i < count; i++) {
	    final View v=view.findViewById(to[i]);
	    if (v != null) {
		if (Const.Subst.KEY_TEAM.equals(myFrom[i])) {
		    text=Const.HOME_TEAM == cursor.getInt(from[i]) ? activity.getTeam1Name().toString() : activity
			.getTeam2Name().toString();
		}
		else if (Const.Subst.KEY_HALF.equals(myFrom[i])) {
		    text=r.getTextArray(R.array.short_halfs)[cursor.getInt(from[i])].toString();
		}
		else {
		    text=cursor.getString(from[i]);
		}
		if (v instanceof TextView) {
		    setViewText((TextView) v, text);
		}
		else if (v instanceof ImageView) {
		    setViewImage((ImageView) v, text);
		}
		else {
		    throw new IllegalStateException(v.getClass().getName() + " is not a "
			+ " view that can be bounds by this SimpleCursorAdapter");
		}
	    }
	}
    }

}